package com.example.service.impl;

import com.example.entity.Role;
import com.example.entity.User;
import com.example.service.RoleService;
import com.example.service.ShiroService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;

//
@Service
public class ShiroServiceImpl implements ShiroService {
    @Autowired
    @Lazy
    private UserService userService;
    @Autowired
    @Lazy
    private RoleService roleService;

    @Override
    public Map<String, Collection<String>> getRolePermissionMap() {
        List<Role> roles = roleService.list();
        Map<String, Collection<String>> map = new HashMap<>();

        for (Role role : roles) {
            List<Long> permissions = role.getPermission();

            Collection<String> permissionStrings = new HashSet<>();
            for (Number permission : permissions) {
                permissionStrings.add(Long.toString(permission.longValue()));
            }
            map.put(role.getId().toString(), permissionStrings);
        }
        return map;
    }

    @Override
    public User queryUserByName(String username) {

        return userService.getbyUsername(username);
    }
}
